package priv.happy.mealpickpro.aspect;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import priv.happy.mealpickpro.domain.Tag;
import priv.happy.mealpickpro.domain.UserMeal;
import priv.happy.mealpickpro.dto.UserDTO;
import priv.happy.mealpickpro.service.TagService;
import priv.happy.mealpickpro.service.UserMealService;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: HAPPY
 * @Project_name: mealPickPro
 * @Package_name: priv.happy.mealpickpro.aspect
 * @Date: 2023/10/27 20:36
 * @Description:
 */
@Component
@Aspect
public class RegisterAspect {

    @Autowired
    TagService tagService;

    @Autowired
    UserMealService userMealService;

    @Around(value = "execution(* priv.happy.mealpickpro.service.impl.AuthenticationServiceImpl.register(..))")
    public Object pickLog(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        int res = 0;
        try {
            // 获取执行参数
            Object[] args = proceedingJoinPoint.getArgs();
            UserDTO userDTO = (UserDTO) args[0];

            // 得到执行结果，是一个FoodVO
            res = (Integer) proceedingJoinPoint.proceed();
            // 注册成功添加默认的几个标签
            if (res == 0) {
                UserMeal userMeal = userMealService.getOne(new QueryWrapper<UserMeal>()
                        .eq("email", userDTO.getEmail()));
                List<Tag> tagList = new ArrayList<>();
                tagList.add(new Tag().setName("米饭").setPostTime(DateUtil.now()).setType("ingredients").setPostUserId(userMeal.getId()));
                tagList.add(new Tag().setName("面条").setPostTime(DateUtil.now()).setType("ingredients").setPostUserId(userMeal.getId()));
                tagList.add(new Tag().setName("饼").setPostTime(DateUtil.now()).setType("ingredients").setPostUserId(userMeal.getId()));
                tagList.add(new Tag().setName("辛辣").setPostTime(DateUtil.now()).setType("taste").setPostUserId(userMeal.getId()));
                tagList.add(new Tag().setName("清淡").setPostTime(DateUtil.now()).setType("taste").setPostUserId(userMeal.getId()));
                tagList.add(new Tag().setName("油炸").setPostTime(DateUtil.now()).setType("taste").setPostUserId(userMeal.getId()));
                tagList.add(new Tag().setName("出去吃").setPostTime(DateUtil.now()).setType("others").setPostUserId(userMeal.getId()));
                tagList.add(new Tag().setName("自己做").setPostTime(DateUtil.now()).setType("others").setPostUserId(userMeal.getId()));
                tagList.add(new Tag().setName("点外卖").setPostTime(DateUtil.now()).setType("others").setPostUserId(userMeal.getId()));
                tagService.saveBatch(tagList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return res;
    }
}
